﻿
package extremefx.ui {
	import extremefx.drawing.geometry.RectangleShape;
	import extremefx.events.EventHandler;
	import extremefx.ui.AnimatedComponent;
	import extremefx.ui.dnd.DragEvent;
	import extremefx.ui.dnd.DragManager;
	import extremefx.ui.dnd.IDragSource;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;		

	/**
	 * @author Marcelo Volmaro
	 */
	public class DragDockPanel extends AnimatedComponent implements IDragSource {
		public static var MAXIMIZED:uint = 1;
		public static var MINIMIZED:uint = 2;
		public static var NORMAL:uint = 3;
		
		private var _myrec1:RectangleShape;
		private var _myrec2:RectangleShape;
		private var _state:uint;
		
		
		public function DragDockPanel(pTitle:String = "", pParameters:UIParameters = null) {
			super();
			pParameters = initStyle("DragDockPanel", pParameters);
			
			_myrec1 = new RectangleShape();
			_myrec1.color = 0xff0000;
			_myrec1.cornerRadius = 10;
			_visualUI.addChild(_myrec1);
			
			_myrec2 = new RectangleShape();
			_myrec2.color = 0x00ff00;
			_myrec2.cornerRadius = 10;
			_visualUI.addChild(_myrec2);

			var tf:TextField = _tf = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.condenseWhite = true;
			tf.selectable = false;
			
			var tformat:TextFormat = new TextFormat("Verdana", 12);
			tf.defaultTextFormat = tformat;
			tf.text = this.toString();
			_visualUI.addChild(tf);
			
			_myrec2.addEventListener(MouseEvent.CLICK, _maxMin, false, 0, true);
			
			state = NORMAL;
		}
		
		private function _startCmpDrag(event:MouseEvent):void {
			event.stopPropagation();
			DragManager.startDrag(this);
		}

		private function _maxMin(event:MouseEvent):void {
			if (state == MAXIMIZED){
				onMinimize.fire();
				
			} else {
				onMaximize.fire();
			}
		}
		
		private function _stopDrag(pSender:DragDockPanel, pArgs:DragEvent):void {
			this.visible = true;
		}

		private function _startDrag(pSender:DragDockPanel, pArgs:DragEvent):void {
			this.visible = false;
		}
		
		private var _tf:TextField;
		private var _eDrag:Boolean;

		protected override function _draw():void {
			_visualUI.x = _x;
			_visualUI.y = _y;
			_myrec1.width = _myrec2.width = _width;
			_myrec1.height = _myrec2.y = _myrec2.height = _height / 2;
		}
		
		private var _eOnMinimize:EventHandler;
		public function get onMinimize():EventHandler{
			if (_eOnMinimize == null) _eOnMinimize = new EventHandler(this);
			return _eOnMinimize;
		}
		
		private var _eOnMaximize:EventHandler;
		public function get onMaximize():EventHandler{
			if (_eOnMaximize == null) _eOnMaximize = new EventHandler(this);
			return _eOnMaximize;
		}
		
		private var _eOnDragStart:EventHandler;
		public function get onDragStart():EventHandler{
			if (_eOnDragStart == null) _eOnDragStart = new EventHandler(this);
			return _eOnDragStart;
		}
		
		private var _eOnDragStop:EventHandler;
		public function get onDragStop():EventHandler{
			if (_eOnDragStop == null) _eOnDragStop = new EventHandler(this);
			return _eOnDragStop;
		}
		
		private var _eOnDragMove:EventHandler;
		public function get onDragMove():EventHandler{
			if (_eOnDragMove == null) _eOnDragMove = new EventHandler(this);
			return _eOnDragMove;
		}
		
		public function get state():uint {
			return _state;
		}
		
		public function set state(pState:uint):void {
			_state = pState;
			_enableDrag(pState == NORMAL);
		}
		
		private function _enableDrag(pEnable:Boolean):void {
			if (pEnable == _eDrag) return;
			_eDrag = pEnable;
			
			if (pEnable){
				_myrec1.addEventListener(MouseEvent.MOUSE_DOWN, _startCmpDrag, false, 0, true);
				
				onDragStart.add(_startDrag);
				onDragStop.add(_stopDrag);
			
			} else {
				_myrec1.removeEventListener(MouseEvent.MOUSE_DOWN, _startCmpDrag);
				
				onDragStart.remove(_startDrag);
				onDragStop.remove(_stopDrag);
			}
		}
	}
}
